package com.nbjtjc.safe.service.danger;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.danger.DangerSafePlanListMapper;
import com.nbjtjc.safe.model.danger.DangerSafePlanList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @auther guqianbin
 * @date 2018/4/10 10:08
 **/
@Service
public class DangerSafePlanListService {
    @Autowired
    private DangerSafePlanListMapper dangerSafePlanListMapper;

    public Result find(DangerSafePlanList dangerSafePlanList){
        Result result=new Result();
        PageHelper.startPage(dangerSafePlanList.getPageCurrent(),100);

        List<DangerSafePlanList> dangerSafePlanLists=dangerSafePlanListMapper.search(dangerSafePlanList);
        dangerSafePlanLists.sort(new Comparator<DangerSafePlanList>() {
            @Override
            public int compare(DangerSafePlanList o1, DangerSafePlanList o2) {
                return o1.getInspectDate().compareTo(o2.getInspectDate());
            }
        });
        if(CollectionUtil.isEmpty(dangerSafePlanLists)){
            result.setData(new PageInfo<>(new ArrayList<>()));
        }else {
            result.setData(new PageInfo<>(dangerSafePlanLists));
        }
        return result;
    }

    public Result delete(DangerSafePlanList dangerSafePlanList){
        Result result=new Result();
        dangerSafePlanListMapper.deleteByPrimaryKey(dangerSafePlanList);
        return result;
    }
    public Result update(DangerSafePlanList dangerSafePlanList){
        Result result=new Result();
        dangerSafePlanListMapper.updateByPrimaryKeySelective(dangerSafePlanList);
        result.setData(dangerSafePlanList);
        return result;
    }

    public Result add(DangerSafePlanList dangerSafePlanList){
        Result result=new Result();
        dangerSafePlanListMapper.insertSelective(dangerSafePlanList);
        result.setData(dangerSafePlanList);
        return result;
    }
}
